//
//  FirstMainViewController.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/11/23.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "FirstMainViewController.h"
#import "AFNetworkHandler.h"
#import "FirstModel.h"
#import "FirstMainTableViewCell.h"
#import "FirstOtherTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "FirstTopViewViewController.h"

@interface FirstMainViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableDataArr;
@property (nonatomic, retain) NSMutableArray *topViewArr;

@property (nonatomic, retain) UITableView *otherTableView;
@property (nonatomic, retain) NSMutableArray *otherTableDataArr;


@property (nonatomic, retain) UISegmentedControl *seg;

@end

@implementation FirstMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableDataArr = [NSMutableArray array];
    self.otherTableDataArr = [NSMutableArray array];
    self.topViewArr = [NSMutableArray array];
    
    //创建tableView
    [self createFirstMainTableView];
    [self createFirstOtherTableView];
    //获取tableView数据
    [self getFirstMainTableData];
    [self getFirstOtherTableData];
    
    [self initNavigationView];
    
}

- (void)initNavigationView
{
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"精选", @"专栏"]];
    self.seg.selectedSegmentIndex = 0;
    self.seg.tintColor = [UIColor whiteColor];
    self.seg.frame = CGRectMake(0, 0, 100, 30);
    [self.seg addTarget:self action:@selector(changeTableView:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.seg;
}

- (void)changeTableView:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0) {
        
        [UIView animateWithDuration:0.6 animations:^{
            
            self.tableView.frame = CGRectMake(0, 0, 375, 667 - 44);
            self.otherTableView.frame = CGRectMake(375, 0, 375, 667 - 44);
        }];
        
    } else {
        
        [UIView animateWithDuration:0.6 animations:^{
           
            self.otherTableView.frame = CGRectMake(0, 0, 375, 667 - 44);
            self.tableView.frame = CGRectMake(-375, 0, 375, 667 - 44);
        }];
    }
}

#pragma mark - 第一个tableView
- (void)getFirstMainTableData
{
    [AFNetworkHandler getUrl:@"http://api.lis99.com/v2/club/omnibusList/0/" cookie:nil result:^(id result) {
        NSArray *arr = result[@"data"][@"omnibuslist"];
        self.tableDataArr = [FirstModel transformWithArray:arr];
        [self.tableView reloadData];
    }];
    
    [AFNetworkHandler getUrl:@"http://api.lis99.com/v3/club/index/?user_id=(null)?" cookie:nil result:^(id result) {
        
        self.topViewArr = result[@"data"][@"banners"];
        
        [self createHeaderViewWithArray:self.topViewArr];
        
    }];
 
}

- (void)createHeaderViewWithArray:(NSArray *)array
{
    UIView *headView = [self.view viewWithTag:1002];
    UIScrollView *headScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 170)];
    headScroll.tag = 2000;
    headScroll.contentSize = CGSizeMake((array.count + 1) * 375, 0);
    headScroll.delegate = self;
    headScroll.backgroundColor = [UIColor whiteColor];
    headScroll.pagingEnabled = YES;
    headScroll.bounces = YES;
    headScroll.showsHorizontalScrollIndicator = NO;
    [headView addSubview:headScroll];
    
    
    NSArray *pageArr = @[@"①", @"②", @"③", @"④", @"⑤"];
    for (NSInteger i = 0; i < array.count; i++) {
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * 375, 0, 375, 170)];
        
        UILabel *pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * 375 + 350, 150, 30, 20)];
        pageLabel.textColor = [UIColor whiteColor];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(i * 375, 0, 375, 170);
        [button addTarget:self action:@selector(pushToTopVC:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i < 4) {
            
            [imgView sd_setImageWithURL:[NSURL URLWithString:array[i][@"images"]]];
            pageLabel.text = pageArr[i];
            button.tag = 3000 + i;
            
        } else if(i == 4) {
            
            [imgView sd_setImageWithURL:[NSURL URLWithString:array[0][@"images"]]];
            pageLabel.text = pageArr[0];
            button.tag = 3005;
            
        }
        
        [headScroll addSubview:imgView];
        [headScroll addSubview:pageLabel];
        [headScroll addSubview:button];
    }
}


- (void)createFirstMainTableView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 205)];
    headerView.tag = 1002;
    [self.view addSubview:headerView];
    
    UILabel *colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 175, 10, 30)];
    colorLabel.backgroundColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
    [headerView addSubview:colorLabel];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(33, 175, 150, 30)];
    titleLabel.text = @"精彩游记和专题";
    titleLabel.textColor = [UIColor lightGrayColor];
    [headerView addSubview:titleLabel];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 44) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = headerView;

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeToOtherView)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.tableView addGestureRecognizer:swipe];
    
    [self.tableView registerClass:[FirstMainTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)changeToOtherView
{
    [UIView animateWithDuration:0.6 animations:^{
        
        self.seg.selectedSegmentIndex = 1;
        self.otherTableView.frame = CGRectMake(0, 0, 375, 667 - 44);
        self.tableView.frame = CGRectMake(-375, 0, 375, 667 - 44);
    }];

}

#pragma mark - 第二个tableView
- (void)getFirstOtherTableData
{
    
    [AFNetworkHandler getUrl:@"http://api.lis99.com/v3/club/taglist/0/" cookie:nil result:^(id result) {
        
        NSArray *arr = result[@"data"][@"taglist"];
        self.otherTableDataArr = [FirstModel transformWithArray:arr];
        [self.otherTableView reloadData];

    }];
    
}

- (void)createFirstOtherTableView
{
    self.otherTableView = [[UITableView alloc] initWithFrame:CGRectMake(375, 0, 375, 667 - 44) style:UITableViewStylePlain];
    self.otherTableView.tag = 1001;
    [self.view addSubview:self.otherTableView];
    self.otherTableView.delegate = self;
    self.otherTableView.dataSource = self;
    self.otherTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeToMainView)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.otherTableView addGestureRecognizer:swipe];
    
    [self.otherTableView registerClass:[FirstOtherTableViewCell class] forCellReuseIdentifier:@"otherCell"];
}

- (void)changeToMainView
{
    [UIView animateWithDuration:0.6 animations:^{
        
        self.seg.selectedSegmentIndex = 0;
        self.tableView.frame = CGRectMake(0, 0, 375, 667 - 44);
        self.otherTableView.frame = CGRectMake(375, 0, 375, 667 - 44);
    }];
}


#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 1001) {
        return self.otherTableDataArr.count;
    } else {
        return self.tableDataArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView.tag == 1001) {
        FirstOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"otherCell"];
        cell.otherFirstModel = self.otherTableDataArr[indexPath.row];
        return cell;
    } else {
        
        FirstMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.firstModel = self.tableDataArr[indexPath.item];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

#pragma mark - scrollView delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:2000];
    if (scrollView == scroll) {
        if (scrollView.contentOffset.x / 375 == 4) {
            scrollView.contentOffset = CGPointMake(0, 0);
        }
    }
}



#pragma amrk - 轮播图点击方法
- (void)pushToTopVC:(UIButton *)sender
{
    FirstTopViewViewController *topVC = [[FirstTopViewViewController alloc] init];
    topVC.idString = self.topViewArr[sender.tag - 3000][@"contents"];
    [self.navigationController pushViewController:topVC animated:YES];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
